package io.modifier.basic.emo.permission

import android.view.View
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.DisposableEffectResult
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalView
import io.modifier.basic.emo.core.EmoLog
import io.modifier.basic.emo.permission.core.PermissionState
import io.modifier.basic.emo.permission.core.PermissionStatus
import io.modifier.basic.emo.permission.core.rememberPermissionState
import kotlinx.coroutines.CoroutineScope

private class EmoPermissionState(
    view: View,
    scope: CoroutineScope,
    tip: EmoPermissionTip,
    val permissionState: PermissionState,
) : PermissionState {

    private val modal = EmoPermissionModal(view, scope, tip)

    override val permission: String
        get() = permissionState.permission
    override val status: PermissionStatus
        get() = permissionState.status

    private var startReqTime: Long = 0L

    override fun launchPermissionRequest() {
        startReqTime = System.currentTimeMillis()
        permissionState.launchPermissionRequest()
        modal.launch()
    }

    fun dealNotAvailableTip(result: Boolean) {
        if (result.not() && (System.currentTimeMillis() - startReqTime) < 800) {
            modal.showNotAvailableTip()
        }
    }

    fun dismiss() {
        modal.dismiss()
    }

}

private class PermissionCallbackActionHolder(
    var action: ((Boolean) -> Unit)? = null,
)

@Composable
fun rememberEmoPermissionState(
    permission: String,
    tipContent: String,
    notAvailableContent: String = "",
    sureBtnString: String = "Sure",
    onPermissionResult: (Boolean) -> Unit = {},
): PermissionState {
    val tip = remember(tipContent) {
        SimpleEmoPermissionTip(tipContent, notAvailableContent, sureBtnString)
    }
    return rememberEmoPermissionState(permission, tip, onPermissionResult)
}

@Composable
fun rememberEmoPermissionState(
    permission: String,
    tip: EmoPermissionTip,
    onPermissionResult: (Boolean) -> Unit = {},
): PermissionState {
    val callbackHolder = remember {
        PermissionCallbackActionHolder()
    }
    val permissionState = rememberPermissionState(permission) {
        EmoLog.d("测试", "权限  result:$it")
        callbackHolder.action?.invoke(it)
        onPermissionResult(it)
    }
    val view = LocalView.current
    val scope = rememberCoroutineScope()
    val emoPermissionState = remember(permissionState, tip) {
        EmoPermissionState(view, scope, tip, permissionState)
    }
    callbackHolder.action = {
        emoPermissionState.dismiss()
        emoPermissionState.dealNotAvailableTip(it)
    }
    DisposableEffect(emoPermissionState) {
        object : DisposableEffectResult {
            override fun dispose() {
                EmoLog.d("测试", "权限  dispose")
                emoPermissionState.dismiss()
            }
        }
    }

    return emoPermissionState
}
